//
//  PickCityView.m
//  New_Runway
//
//  Created by mack on 2018/1/27.
//  Copyright © 2018年 NelsonJ. All rights reserved.
//

#import "PickCityView.h"
#import "PrevinceModel.h"
#import "CityModel.h"
#import "NRWAdressApi.h"
#import "NRWCityApi.h"

@interface PickCityView ()<UIPickerViewDataSource, UIPickerViewDelegate,YTKRequestDelegate>
@property (nonatomic, strong, nullable)NSMutableArray *selectedArray;//当前选中的数组
@property (nonatomic, strong, nullable)NSString *selectProvince;
@property (nonatomic, strong, nullable)NSString *selectProvinceId;
@property (nonatomic, strong, nullable)NSString *selectCity;
@property (nonatomic, strong, nullable)NSString *selectCityId;
@property (nonatomic, strong) NRWAdressApi *provinceApi;
@property (nonatomic, strong) NRWCityApi *cityApi;
@property (nonatomic, strong) PrevinceModel *procinceModel;
@property (nonatomic, strong) CityModel *cityModel;
@end

@implementation PickCityView

-(NRWAdressApi *)provinceApi{
    if (!_provinceApi) {
        _provinceApi = [[NRWAdressApi alloc] init];
        _provinceApi.delegate = self;
    }
    return _provinceApi;
}
-(NRWCityApi *)cityApi{
    if (!_cityApi) {
        _cityApi = [[NRWCityApi alloc] init];
        _cityApi.delegate = self;
    }
    return _cityApi;
}
- (void)initPickView
{
    [super initPickView];
    [self.provinceApi start];
    
    [self.pickerView setDelegate:self];
    [self.pickerView setDataSource:self];
    
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 2;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if (component == 0) {
        return _procinceModel.previnceNames.count;
    }else  {
        return _cityModel.cityNames.count;
    }
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
    return 36;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if (component == 0) {

        self.selectProvince = _procinceModel.previnceNames[row];
        PrevinceResultModel *model = _procinceModel.result[row];
        self.selectProvinceId = model.provinceId;
        self.cityApi.provinceId = model.provinceId;
        [self.cityApi start];

    }else if (component == 1) {
        self.selectCity = _cityModel.cityNames[row];
        cityResultModel *model = _cityModel.result[row];
        self.selectCityId = model.cityId;
    }else{
        
    }
    
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(nullable UIView *)view
{
    for(UIView *singleLine in pickerView.subviews)
    {
        if (singleLine.frame.size.height < 1)
        {
            singleLine.backgroundColor =[UIColor grayColor];
        }
    }
    NSString *text;
    if (component == 0) {
        text =  _procinceModel.previnceNames[row];
    }else if (component == 1){
        
        if (_cityModel.cityNames.count > row) {
            text =  _cityModel.cityNames[row];
        }else{
            
        }
    }else{
        
    }
    
    UILabel *label = [[UILabel alloc]init];
    label.textAlignment = 1;
    label.font = [UIFont systemFontOfSize:16];
    label.text = text;
    
    return label;
}


- (void)clickConfirmButton
{
    
    if ([self.delegate respondsToSelector:@selector(pickerArea:selectProvince:selectCity:)]) {
        
        [self.delegate pickerArea:self selectProvince:self.selectProvince selectCity:self.selectCity];
    }
    
    if ([self.delegate respondsToSelector:@selector(pickerArea:selectProvinceId:selectCityId:)]) {
        
        [self.delegate pickerArea:self selectProvinceId:self.selectProvinceId selectCityId:self.selectCityId];
    }
    
    if ([self.delegate respondsToSelector:@selector(pickerArea:selectProvince:selectCity:selectProvinceId:selectCityId:)]) {
        
        [self.delegate pickerArea:self selectProvince:self.selectProvince selectCity:self.selectCity selectProvinceId:self.selectProvinceId selectCityId:self.selectCityId];
    }
    
    [super clickConfirmButton];
}

#pragma mark - networkDelegate
- (void)requestFinished:(__kindof YTKBaseRequest *)request {
    
    if (request == self.provinceApi) {
        
       _procinceModel = [PrevinceModel mj_objectWithKeyValues:request.responseObject];
        if (_procinceModel.code == 1) {
            [self.pickerView reloadComponent:0];
            self.selectProvince = _procinceModel.previnceNames[0];
            PrevinceResultModel *model = _procinceModel.result[0];
            self.selectProvinceId = model.provinceId;
            self.cityApi.provinceId = model.provinceId;
            [self.cityApi start];
        }else{
            
        }
       
    }
    if (request == self.cityApi) {
        
        _cityModel = [CityModel mj_objectWithKeyValues:request.responseObject];
        if (_cityModel.code == 1) {
            self.selectCity = _cityModel.cityNames[0];
            cityResultModel *model = _cityModel.result[0];
            self.selectCityId = model.cityId;
            [self.pickerView reloadComponent:1];
        }
    }

}

- (void)requestFailed:(__kindof YTKBaseRequest *)request {
    
    
}


- (NSMutableArray *)selectedArray
{
    if (!_selectedArray) {
        _selectedArray = [NSMutableArray array];
    }
    return _selectedArray;
}



@end
